#ifndef HFT_POSITION_MANAGER_H
#define HFT_POSITION_MANAGER_H

#include "execution/execution_types.h"
#include "execution/order_manager.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <chrono>
#include <functional>
#include <sstream>

namespace hft {

class PositionManager {
public:
    PositionManager();
    
    void onTrade(const Trade& trade);
    void updateLastPrice(const std::string& symbol, double price);
    void updateLastPrices(const std::unordered_map<std::string, double>& prices);
    void calculateUnrealizedPnL(const std::unordered_map<std::string, double>& last_prices);
    void updateMarginRatio(const std::string& symbol, double margin_ratio);
    
    Position* getPosition(const std::string& symbol, const std::string& account, const std::string& strategy_id);
    std::vector<Position*> getAccountPositions(const std::string& account);
    std::vector<Position*> getStrategyPositions(const std::string& strategy_id);
    std::vector<Position*> getSymbolPositions(const std::string& symbol);
    const std::unordered_map<std::string, Position>& getAllPositions() const;
    
    void settle();
    
    void registerPositionCallback(std::function<void(const Position&)> callback);

private:
    std::unordered_map<std::string, Position> positions_;
    std::unordered_map<std::string, double> margin_ratios_;
    std::function<void(const Position&)> position_callback_;
    
    std::string generatePositionKey(const std::string& symbol, const std::string& account, const std::string& strategy_id);
    void updatePosition(const Trade& trade);
    void notifyPositionUpdate(const Position& position);
};

} // namespace hft

#endif // HFT_POSITION_MANAGER_H